package ccmis.finance.voucher.action;

import java.util.List;
import javax.ejb.EJB;

import ccmis.finance.account.bo.SubjectListBo;
import ccmis.finance.account.entity.SubjectList;
import ccmis.finance.voucher.bo.IvvoucherDisassembleBo;
import ccmis.finance.voucher.bo.PmvoucherDisassembleBo;
import ccmis.finance.voucher.bo.VoucherBo;
import ccmis.finance.voucher.bo.VoucherHeadBo;
import ccmis.finance.voucher.entity.IvvoucherDisassemble;
import ccmis.finance.voucher.entity.PmvoucherDisassemble;
import ccmis.finance.voucher.entity.Voucher;
import jfox.platform.infrastructure.SuperAction;
import org.jfox.entity.MappedEntity;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;

@Service(id = "voucherhead")
public class VoucherHeadAction extends SuperAction {
	  @EJB
	  VoucherHeadBo voucherHeadBo;
	  @EJB
	  VoucherBo voucherBo;
	  @EJB
	  IvvoucherDisassembleBo ivvoucherDisassembleBo;
	  @EJB
	  PmvoucherDisassembleBo pmvoucherDisassembleBo;
	  @EJB
	  SubjectListBo subjectBo;
	  /**
	   * 初如化查询投资类凭证
	   * @param actionContext
	   * @throws Exception
	   */
	  @ActionMethod(name="index", successView = "ccmis/finance/invest/invest_input.vhtml")
	   public void indexPage(ActionContext actionContext) throws Exception{
		    List<MappedEntity> voucherHeads = voucherHeadBo.getAllVoucherHeadsByInvest();
	        PageContext pageContext = actionContext.getPageContext();
	        pageContext.setAttribute("voucherHeads", voucherHeads);
	    }
	  /**
	   * 初始化查询付款类凭证
	   * @param actionContext
	   * @throws Exception
	   */
	  @ActionMethod(name="payment", successView = "ccmis/finance/payment/payment_input.vhtml")
	   public void payment(ActionContext actionContext) throws Exception{
		    List<MappedEntity> voucherHeads = voucherHeadBo.getAllVoucherHeadsByPayment();
	        PageContext pageContext = actionContext.getPageContext();
	        pageContext.setAttribute("voucherHeads", voucherHeads);
	    }
	  @ActionMethod(name="selectInvest", successView = "ccmis/finance/invest/invest_select.vhtml")
	   public void selectInvest(ActionContext actionContext) throws Exception{
		   
	    }
	  @ActionMethod(name="selectPayment", successView = "ccmis/finance/payment/payment_select.vhtml")
	   public void selectPayment(ActionContext actionContext) throws Exception{
		   
	    }
	  /**
	   * 根据pz_id查询凭证(投资类)
	   * @param actionContext
	   * @throws Exception
	   */
	  @ActionMethod(name="disassemble", successView ="ccmis/finance/invest/invest_voucher_dispart.vhtml",invocationClass = DisassembleVoucherHead.class)
	   public void disassemble(ActionContext actionContext) throws Exception{
		  DisassembleVoucherHead invocation = (DisassembleVoucherHead)actionContext.getInvocation();
	      String pzId=invocation.getPzId().trim();
	      System.out.print(pzId);
	      List<Voucher> vouchers = voucherBo.getVoucherByPzId(pzId);
	      PageContext pageContext = actionContext.getPageContext();
	      pageContext.setAttribute("vouchers", vouchers);
	  }
	  /**
	   * 根据pz_id查询凭证(付款类)
	   * @param actionContext
	   * @throws Exception
	   */
	  @ActionMethod(name="disassemblePayment", successView ="ccmis/finance/payment/payment_voucher_dispart.vhtml",invocationClass = DisassembleVoucherHead.class)
	   public void disassemblePayment(ActionContext actionContext) throws Exception{
		  DisassembleVoucherHead invocation = (DisassembleVoucherHead)actionContext.getInvocation();
	      String pzId=invocation.getPzId().trim();
	      List<Voucher> vouchers = voucherBo.getVoucherByPzId(pzId);
	      PageContext pageContext = actionContext.getPageContext();
	      pageContext.setAttribute("vouchers", vouchers);
	  }
	  /**
	    * 根据条件查询凭证信息(投资类)
	    * @param actionContext
	    * @throws Exception
	    */
	    @ActionMethod(name = "listInvest", successView = "include/json_response.vhtml",invocationClass = ListVoucherHead.class)
	    public void listInvestVoucherHeads(ActionContext actionContext) throws Exception {
		    ListVoucherHead invocation = (ListVoucherHead)actionContext.getInvocation();
			String start = invocation.getStart();
			String end = invocation.getEnd();
			String sid = invocation.getSubjectId();
			String type = invocation.getType();
			boolean ftest = false;
			List<MappedEntity> voucherHeads =null;
            if(invocation.getFinishSearch()!= null)
				ftest = true;
            if(!start.equals("") && !end.equals("")){
			   if(sid != "" && !sid.equals("")) 
				  voucherHeads = voucherHeadBo.getAllVoucherHeadsByInvest();
			   else
				  voucherHeads = voucherHeadBo.getAVoucherHeadByTime(start,end,type,ftest);

            }else{
			   if(sid != "" && !sid.equals(""))
				  voucherHeads = voucherHeadBo.getAllVoucherHeadsByInvest();
			   else
				  voucherHeads = voucherHeadBo.getAllVoucherHeadsByInvest();
   		    }
            actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, voucherHeads);
		    
	    }
	    /**
	     * 根据条件查询凭证信息(付款类)
	     * @param actionContext
	     * @throws Exception
	     */
	    @ActionMethod(name = "listPayment", successView = "include/json_response.vhtml",invocationClass = ListVoucherHead.class)
	    public void listPaymentVoucherHeads(ActionContext actionContext) throws Exception {
		    ListVoucherHead invocation = (ListVoucherHead)actionContext.getInvocation();
			String start = invocation.getStart();
			String end = invocation.getEnd();
			String sid = invocation.getSubjectId();
			String type = invocation.getType();
			boolean ftest = false;
			List<MappedEntity> voucherHeads =null;
            if(invocation.getFinishSearch()!= null)
				ftest = true;
            if(!start.equals("") && !end.equals("")){
			   if(sid != "" && !sid.equals("")) 
				  voucherHeads = voucherHeadBo.getAllVoucherHeadsByPayment();
			   else
				  voucherHeads = voucherHeadBo.getAVoucherHeadByTime(start,end,type,ftest);

            }else{
			   if(sid != "" && !sid.equals(""))
				  voucherHeads = voucherHeadBo.getAllVoucherHeadsByPayment();
			   else
				  voucherHeads = voucherHeadBo.getAllVoucherHeadsByPayment();
   		    }
            actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, voucherHeads);
		    
	    }
	   /**
	    * 根据凭证编号，分录取得明细(投资类)
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "listDisassemble", successView = "include/json_response.vhtml",invocationClass = ListDisassemble.class)
	    public void listDisassembles(ActionContext actionContext) throws Exception {
		   ListDisassemble invocation = (ListDisassemble)actionContext.getInvocation();
			String voucherId = invocation.getPzId().trim();
			String dispartNo = invocation.getFlh().trim();
		    List<IvvoucherDisassemble> IvvoucherDisassembles =ivvoucherDisassembleBo.getIVDisassembleByVidDno(voucherId, dispartNo);
            actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, IvvoucherDisassembles);
		    
	    }
	   /**
	    * 根据凭证编号，分录取得明细(付款类)
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "listDisassemblePayment", successView = "include/json_response.vhtml",invocationClass = ListDisassemble.class)
	    public void listDisassemblePayment(ActionContext actionContext) throws Exception {
		   ListDisassemble invocation = (ListDisassemble)actionContext.getInvocation();
			String voucherId = invocation.getPzId().trim();
			String dispartNo = invocation.getFlh().trim();
		    List<PmvoucherDisassemble> PmvoucherDisassembles =pmvoucherDisassembleBo.getPmDisassembleByVidDno(voucherId, dispartNo);
           actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, PmvoucherDisassembles);
		    
	    }
	   /**
	    * 获取凭证是否投资类
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "listSubject", successView = "include/json_response.vhtml",invocationClass = ListDisassemble.class)
	    public void listSubject(ActionContext actionContext) throws Exception {
		   ListDisassemble invocation = (ListDisassemble)actionContext.getInvocation();
		   String resut ="0";
		   String subjectId =invocation.getKmdm().trim();
           SubjectList subjectList =subjectBo.getSubjectListBySubjectId(subjectId);
           if(subjectList != null){
        	   if (subjectList.getInvset() != null && !subjectList.getInvset().equals(0)) {
        		   resut ="1";
        	   }
           }
        	actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ,resut);
		    
	    }
	   /**
	    * 获取凭证是否付款类
	    * @param actionContext
	    * @throws Exception
	    */
	   @ActionMethod(name = "listSubjectPayment", successView = "include/json_response.vhtml",invocationClass = ListDisassemble.class)
	    public void listSubjectPayment(ActionContext actionContext) throws Exception {
		   ListDisassemble invocation = (ListDisassemble)actionContext.getInvocation();
		   String resut ="0";
		   String subjectId =invocation.getKmdm().trim();
           SubjectList subjectList =subjectBo.getSubjectListBySubjectId(subjectId);
           if(subjectList != null){
        	   if (subjectList.getPayment() != null && !subjectList.getPayment().equals(0)) {
        		   resut ="1";
        	   }
           }
        	actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ,resut);
		    
	    }
	    public static class ListVoucherHead extends Invocation {
	    	//开始时间
	    	private String start;
	    	//结束时间
	    	private String end;
	    	//科目
	    	private String subjectId;
	    	//类型
	    	private String type;
	    	//是否进行预分解
	    	private String preDisassemble;
	    	//显示未分解完毕项目
	    	private String finishSearch;
			public String getStart() {
				return start;
			}
			public void setStart(String start) {
				this.start = start;
			}
			public String getEnd() {
				return end;
			}
			public void setEnd(String end) {
				this.end = end;
			}
			public String getSubjectId() {
				return subjectId;
			}
			public void setSubjectId(String subjectId) {
				this.subjectId = subjectId;
			}
			public String getType() {
				return type;
			}
			public void setType(String type) {
				this.type = type;
			}
			public String getPreDisassemble() {
				return preDisassemble;
			}
			public void setPreDisassemble(String preDisassemble) {
				this.preDisassemble = preDisassemble;
			}
			public String getFinishSearch() {
				return finishSearch;
			}
			public void setFinishSearch(String finishSearch) {
				this.finishSearch = finishSearch;
			}
	    	
	    }
	    public static class DisassembleVoucherHead extends Invocation{
	    	//凭证编号
	    	private String pzId;

			public String getPzId() {
				return pzId;
			}

			public void setPzId(String pzId) {
				this.pzId = pzId;
			}
	    	
	    }
	    public static class ListDisassemble extends Invocation{
	    	//凭证编号
	    	private String pzId;
	    	//分录号
	    	private String flh;
	    	
	    	private String kmdm;

			public String getKmdm() {
				return kmdm;
			}

			public void setKmdm(String kmdm) {
				this.kmdm = kmdm;
			}

			public String getPzId() {
				return pzId;
			}

			public void setPzId(String pzId) {
				this.pzId = pzId;
			}
			
			public String getFlh() {
				return flh;
			}

			public void setFlh(String flh) {
				this.flh = flh;
			}
	    	
	    }

}